package com.gjy.datetime;

import org.junit.jupiter.api.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-03-22 15:28:34
 */
public class NewTimeTest {

    @Test
    public void test6() {
        LocalDateTime datetime = LocalDateTime.now();
        System.out.println(datetime.format(DateTimeFormatter.BASIC_ISO_DATE));
        System.out.println(datetime.format(DateTimeFormatter.ISO_DATE_TIME));
    }

    @Test
    public void test5() {
        LocalDateTime date = LocalDateTime.now();

        LocalDateTime with = date.with(temporal -> {
            DayOfWeek dow = DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK));
            int dayAdd = 1;
            if (dow == DayOfWeek.FRIDAY) {
                dayAdd = 3;
            } else if (dow == DayOfWeek.SATURDAY) {
                dayAdd = 2;
            }
            return temporal.plus(dayAdd, ChronoUnit.DAYS);
        });
        System.out.println("with = " + with);
    }

    @Test
    public void test4() {
        LocalDateTime now = LocalDateTime.now();
        now.with(TemporalAdjusters.lastDayOfMonth());
    }

    @Test
    public void test3() {
        Period until = LocalDate.now()
                .until(LocalDate.of(2023, 3, 23));
        System.out.println("until.getDays() = " + until.getDays());
    }

    @Test
    public void test2() {
        Duration between = Duration.between(Instant.ofEpochSecond(10), Instant.ofEpochSecond(3));
        System.out.println("between = " + between.getSeconds());
    }

    @Test
    public void test1() {
        ZonedDateTime zone = Instant.ofEpochSecond(System.currentTimeMillis() / 1000)
//                .atZone(ZoneId.of("+8"));
                .atZone(ZoneOffset.ofHours(8));
        System.out.println("instant = " + zone);
    }
}
